﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.ServiceProcess;

namespace WinNMPForms
{
    public partial class Wnmp : Form
    {
        protected bool viewSiteShow = false;

        internal Nginx LocalNginx { get; private set; }

        internal PhpCgi LocalPhp { get; private set; }

        internal Mysql LocalMysql  { get; private set; }

    public Wnmp()
        {
            InitializeComponent();
            this.LocalNginx = new Nginx();
            this.LocalPhp = new PhpCgi();
            this.LocalMysql = new Mysql();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 读取配置文件检查nginx,php,mysql安装位置,版本及运行状态
            if (this.LocalNginx.IsExits == false)
            {
                this.labelNginxDir.Text = "不存在";
            }
            else
            {
                //this.labelNginxDir.Text =  this.LocalNginx.Path;
                LTooltip(this.labelNginxDir, 41, this.LocalNginx.Path);
                this.labelNginxVersion.Text = this.LocalNginx.Version;
                LabelStatus(this.labelStatusNginx, this.LocalNginx.IsRun);
            }

            if (this.LocalPhp.IsExits == false)
            {
                this.labelPhpDir.Text = "不存在";
            }
            else
            {
                LTooltip(this.labelPhpDir, 41, this.LocalPhp.Path);
                this.labelPhpVersion.Text = this.LocalPhp.Version;
                LabelStatus(this.labelStatusPhp, this.LocalPhp.IsRun);
            }

            if (string.IsNullOrEmpty(this.LocalMysql.ServerName))
            {
                this.labelMysqlDir.Text = "服务未配置";
            } 
            else if (this.LocalMysql.IsExits == false)
            {
                this.labelMysqlDir.Text = this.LocalMysql.ServerName + " 服务不存在";
            }
            else
            {
               this.labelMysqlDir.Text = this.LocalMysql.ServerName;
               LabelStatus(this.labelStatusMysql, this.LocalMysql.IsRun);
            }


            // 已配置站点
            GetViewSite();
        }

        // 运行状态显示
        private void LabelStatus(System.Windows.Forms.Label label, bool statuus)
        {
            if (statuus == false)
            {
                label.Text = "停止";
                label.ForeColor = Color.OrangeRed;
            }
            else
            {
                label.Text = "运行";
                label.ForeColor = Color.LimeGreen;
            }
        }

        public void LTooltip(System.Windows.Forms.Label label, int length, string value)
        {
            label.Text = value;
            if (value.Length > length)
            {
                label.Text = label.Text.Substring(0, length) + "...";
            }
            var tip = new ToolTip
            {
                IsBalloon = false,
                ShowAlways = true
            };
            tip.SetToolTip(label, value);
        }

        public void AddTextToLogBox(string msg)
        { 
            this.logTextBox.Text += msg;
            this.logTextBox.Text += "\n";
        }

        private void ButtonRunAll_Click(object sender, EventArgs e)
        {
            AddTextToLogBox("wnmp启动中...");
            if (this.LocalNginx.IsExits == true)
            {
                if (this.LocalNginx.IsRun == true)
                {
                    AddTextToLogBox("nginx已启动...");
                }
                else
                {
                    AddTextToLogBox("nginx启动中...");
                    string output = this.LocalNginx.CheckConf();
                    AddTextToLogBox(output);
                    this.LocalNginx.Start();
                    AddTextToLogBox("nginx已启动");
                    LabelStatus(this.labelStatusNginx, true);
                    GetViewSite();
                }
            }
            else
            {
                AddTextToLogBox("未找到nginx服务,请检查配置...");
            }

            if (this.LocalPhp.IsExits == true)
            {
                if (this.LocalPhp.IsRun == true)
                {
                    AddTextToLogBox("php-cgi已启动...");
                }
                else
                {
                    AddTextToLogBox("php-cgi启动中...");
                    this.LocalPhp.Start();
                    LabelStatus(this.labelStatusPhp, true);
                    AddTextToLogBox("php-cgi已启动");
                }
            }
            else
            {
                AddTextToLogBox("未找到php-cgi服务,请检查配置...");
            }

            if (this.LocalMysql.IsExits == true)
            {
                ServiceController service = new ServiceController(this.LocalMysql.ServerName);
                if ((service.Status.Equals(ServiceControllerStatus.Stopped)) ||
                    (service.Status.Equals(ServiceControllerStatus.StopPending)))
                {
                    // Start the service if the current status is stopped.
                    Console.WriteLine("Starting the Mysql service...");
                    AddTextToLogBox("mysql启动中...");
                    service.Start();
                    LabelStatus(this.labelStatusMysql, true);
                    AddTextToLogBox("mysql已启动");
                    this.LocalMysql.IsRun = true;
                }
                else
                {
                    // Stop the service if its status is not set to "Stopped".
                   
                    AddTextToLogBox("mysql服务运行中...");
                }
                service.Close();
            }
            else
            {
                AddTextToLogBox("mysql服务不存在,请检查配置...");
            }
        }

        private void ButtonStopAll_Click(object sender, EventArgs e)
        {
            AddTextToLogBox("wnmp停止中...");

            // 停止各项服务
            if (this.LocalNginx.IsExits == true)
            {
                if (this.LocalNginx.IsRun == true)
                {
                    AddTextToLogBox("nginx停止中...");
                    this.LocalNginx.Stop();
                    Thread.Sleep(1000);
                    AddTextToLogBox("nginx已停止中");
                    LabelStatus(this.labelStatusNginx, false);
                }
            }
            else
            {
                AddTextToLogBox("未找到nginx服务,请检查配置...");
            }

            if (this.LocalPhp.IsExits == true)
            {
                if (this.LocalPhp.IsRun == true)
                {
                    AddTextToLogBox("php-cgi停止中...");
                    this.LocalPhp.Stop();
                    Thread.Sleep(1000);
                    LabelStatus(this.labelStatusPhp, false);
                    AddTextToLogBox("php-cgi已停止中");
                }
            }
            else
            {
                AddTextToLogBox("未找到php-cgi服务,请检查配置...");
            }

            if (this.LocalMysql.IsExits == true)
            {
                ServiceController service = new ServiceController(this.LocalMysql.ServerName);

                if (service.Status.Equals(ServiceControllerStatus.Running))
                {
                    service.Stop();

                    AddTextToLogBox("mysql停止中...");
                    LabelStatus(this.labelStatusMysql, false);
                    AddTextToLogBox("mysql已停止中");
                    this.LocalMysql.IsRun = false;

                    service.Close();
                }
            }
            else
            {
                AddTextToLogBox("mysql服务不存在,请检查配置...");
            }

            AddTextToLogBox("wnmp已停止");
        }

        private void GetViewSite()
        {
            // 已配置站点
            List<string> linkList = this.LocalNginx.ViewSite();

            int linksStart = 0;

            this.linkLabelViewSite.Text = "";
            linkLabelViewSite.Links.Clear();

            foreach (string link in linkList)
            {
                string links = link + "\n";

                this.linkLabelViewSite.Text += links;
                linkLabelViewSite.Links.Add(linksStart, links.Length, link);
                linksStart += links.Length;
                Console.WriteLine(linksStart);

            }
        }

        private void ButtontStartNginx_Click(object sender, EventArgs e)
        {
            if (this.LocalNginx.IsExits == true)
            {
                if (this.LocalNginx.IsRun == true)
                {
                    AddTextToLogBox("nginx停止中...");
                    this.LocalNginx.Stop();
                    Thread.Sleep(1000);
                    AddTextToLogBox("nginx已停止中");
                    LabelStatus(this.labelStatusNginx, false);
                }
                else
                {
                    AddTextToLogBox("nginx启动中...");
                    string output = this.LocalNginx.CheckConf();
                    AddTextToLogBox(output);
                    this.LocalNginx.Start();
                    AddTextToLogBox("nginx已启动");
                    LabelStatus(this.labelStatusNginx, true);
                }
            }
            else
            {
                AddTextToLogBox("未找到nginx服务,请检查配置...");
            }
        }

        private void ButtontRestartNginx_Click(object sender, EventArgs e)
        {
            if (this.LocalNginx.IsExits == true)
            {
                AddTextToLogBox("nginx停止中...");
                this.LocalNginx.Stop();
                AddTextToLogBox("nginx已停止中");
                LabelStatus(this.labelStatusNginx, false);
                Thread.Sleep(1000);

                AddTextToLogBox("nginx启动中...");
                string output = this.LocalNginx.CheckConf();
                AddTextToLogBox(output);
                this.LocalNginx.Start();
                LabelStatus(this.labelStatusNginx, true);
                AddTextToLogBox("nginx已启动");

                // 更新站点
                GetViewSite();
            }
            else
            {
                AddTextToLogBox("未找到nginx服务,请检查配置...");
            }
        }

        private void ButtonShowNginxVersion_Click(object sender, EventArgs e)
        {
            string output = this.LocalNginx.ShowVersion();
            AddTextToLogBox(output);
        }

        private void ButtontCheckNginx_Click(object sender, EventArgs e)
        {
            string output = this.LocalNginx.CheckConf();
            AddTextToLogBox(output);
        }

        private void ButtonEditNginxConf_Click(object sender, EventArgs e)
        {
            this.LocalNginx.EditConf();
        }

        private void ButtonViewSite_Click(object sender, EventArgs e)
        {
            this.viewSiteShow = !viewSiteShow;

            this.panelViewSite.Visible = this.viewSiteShow;

        }

        private void LinkLabelViewSiteClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }

        private void ButtontStartPhp_Click(object sender, EventArgs e)
        {
            if (this.LocalPhp.IsExits == true)
            {
                if (this.LocalPhp.IsRun == true)
                {
                    AddTextToLogBox("php-cgi停止中...");
                    this.LocalPhp.Stop();
                    Thread.Sleep(1000);
                    LabelStatus(this.labelStatusPhp, false);
                    AddTextToLogBox("php-cgi已停止中");
                }
                else
                {
                    AddTextToLogBox("php-cgi启动中...");
                    this.LocalPhp.Start();
                    LabelStatus(this.labelStatusPhp, true);
                    AddTextToLogBox("php-cgi已启动");
                }
            }
            else
            {
                AddTextToLogBox("未找到php-cgi服务,请检查配置...");
            }
        }

        private void ButtontRestartPhp_Click(object sender, EventArgs e)
        {
            if (this.LocalPhp.IsExits == true)
            {
                AddTextToLogBox("php-cgi停止中...");
                this.LocalPhp.Stop();
                Thread.Sleep(1000);
                LabelStatus(this.labelStatusPhp, false);
                AddTextToLogBox("php-cgi已停止中");
                AddTextToLogBox("php-cgi启动中...");
                this.LocalPhp.Start();
                LabelStatus(this.labelStatusPhp, true);
                AddTextToLogBox("php-cgi已启动");
            }
            else
            {
                AddTextToLogBox("未找到php-cgi服务,请检查配置...");
            }
        }

        private void buttonShowPhpVersion_Click(object sender, EventArgs e)
        {
            string output = this.LocalPhp.ShowVersion();
            AddTextToLogBox(output);
        }

        private void ButtontStartMysql_Click(object sender, EventArgs e)
        {
            if (this.LocalMysql.IsExits == true)
            {
                ServiceController service = new ServiceController(this.LocalMysql.ServerName);
                if ((service.Status.Equals(ServiceControllerStatus.Stopped)) ||
                    (service.Status.Equals(ServiceControllerStatus.StopPending)))
                {
                    // Start the service if the current status is stopped.
                    Console.WriteLine("Starting the Mysql service...");
                    AddTextToLogBox("mysql启动中...");
                    service.Start();
                    LabelStatus(this.labelStatusMysql, true);
                    AddTextToLogBox("mysql已启动");
                    this.LocalMysql.IsRun = true;
                }
                else
                {
                    // Stop the service if its status is not set to "Stopped".
                    Console.WriteLine("Stopping the Mysql service...");
                    AddTextToLogBox("mysql停止中...");
                    service.Stop();
                    LabelStatus(this.labelStatusMysql, false);
                    AddTextToLogBox("mysql已停止中");
                    this.LocalMysql.IsRun = false;
                }

                service.Close();
            }
            else
            {
                AddTextToLogBox("mysql服务不存在,请检查配置...");
            }
        }

        private void ButtontRestartMysql_Click(object sender, EventArgs e)
        {
            if (this.LocalMysql.IsExits == true)
            {
                ServiceController service = new ServiceController(this.LocalMysql.ServerName);

                if (service.Status.Equals(ServiceControllerStatus.Running))
                {
                    service.Stop();

                    AddTextToLogBox("mysql停止中...");
                    LabelStatus(this.labelStatusMysql, false);
                    AddTextToLogBox("mysql已停止中");
                    this.LocalMysql.IsRun = false;

                    Thread.Sleep(2000);

                    AddTextToLogBox("mysql启动中...");
                    service.Start();
                    LabelStatus(this.labelStatusMysql, true);
                    AddTextToLogBox("mysql已启动");
                    this.LocalMysql.IsRun = true;

                    service.Close();
                }
            }
            else
            {
                AddTextToLogBox("mysql服务不存在,请检查配置...");
            }
        }
    }
}
